package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.*;
import org.example.mapper.*;
import org.example.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private UserRoleCompanyRelationMapper userRoleCompanyRelationMapper;
    
    @Autowired
    private UserMapper userMapper;
    
    @Autowired
    private RoleMapper roleMapper;
    
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    
    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private EmployeeProfileMapper employeeProfileMapper;

    @Override
    public List<Company> getUserCompanies(Long userId) {
        if (userId == null) {
            return new ArrayList<>();
        }
        try {
            return companyMapper.selectCompaniesByUserId(userId);
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }
    
    @Override
    public List<Map<String, Object>> getUserCompanyPermissions(Long userId) {
        if (userId == null) {
            return new ArrayList<>();
        }
        try {
            return companyMapper.selectUserCompanyPermissions(userId);
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    @Override
    public List<Map<String, Object>> getAvailableTenants(Long userId, Long currentCompanyId) {
        if (userId == null) {
            return new ArrayList<>();
        }
        
        System.out.println("=== getAvailableTenants 调试 ===");
        System.out.println("用户ID: " + userId);
        System.out.println("当前租户ID: " + currentCompanyId);
        
        try {
            // 获取用户所有可访问的公司ID
            List<Long> companyIds = userRoleCompanyRelationMapper.getCompanyIdsByUserId(userId);
            System.out.println("用户可访问的所有公司ID: " + companyIds);
            
            List<Map<String, Object>> availableTenants = new ArrayList<>();
            for (Long companyId : companyIds) {
                System.out.println("检查公司ID: " + companyId + ", 是否等于当前租户: " + companyId.equals(currentCompanyId));
                
                // 包含所有公司，不排除当前租户
                System.out.println("公司ID " + companyId + " 添加到可用列表");
                
                // 获取公司信息
                Company company = companyMapper.selectById(companyId);
                if (company != null) {
                    Map<String, Object> tenantInfo = new java.util.HashMap<>();
                    tenantInfo.put("companyId", companyId);
                    tenantInfo.put("companyName", company.getCompanyName());
                    tenantInfo.put("companyCode", company.getCompanyCode());
                    availableTenants.add(tenantInfo);
                    System.out.println("添加公司信息: " + tenantInfo);
                } else {
                    System.out.println("公司ID " + companyId + " 不存在");
                }
            }
            
            System.out.println("最终可用租户列表: " + availableTenants);
            return availableTenants;
        } catch (Exception e) {
            System.err.println("获取可用租户异常: " + e.getMessage());
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    @Override
    public IPage<Company> pageCompanies(Long current, Long size, String keyword, Integer status) {
        if (current == null || current < 1) current = 1L;
        if (size == null || size < 1) size = 10L;
        Page<Company> page = new Page<>(current, size);
        QueryWrapper<Company> qw = new QueryWrapper<>();
        if (keyword != null && !keyword.isEmpty()) {
            qw.lambda().like(Company::getCompanyName, keyword)
                    .or().like(Company::getCompanyCode, keyword);
        }
        if (status != null) {
            qw.lambda().eq(Company::getStatus, status);
        }
        qw.orderByDesc("created_at");
        return companyMapper.selectPage(page, qw);
    }


    @Override
    @Transactional
    public boolean update(Company company) {
        if (company == null || company.getId() == null) return false;
        
        try {
            // 1. 如果修改了管理员手机号，需要重新处理用户关联
            if (company.getAdminUserId() != null && company.getAdminPhone() != null) {
                // 检查新手机号对应的用户是否存在
                User existingUser = userMapper.selectByPhoneAndUserType(company.getAdminPhone(), "企业端");
                
                if (existingUser != null) {
                    // 场景2：新手机号对应的用户已存在，切换到该用户
                    System.out.println("切换到已存在的用户，用户ID: " + existingUser.getId());
                    
                    // 1. 删除原来的管理员档案（如果存在）
                    QueryWrapper<EmployeeProfile> deleteWrapper = new QueryWrapper<>();
                    deleteWrapper.eq("user_id", company.getAdminUserId())
                                 .eq("company_id", company.getId());
                    employeeProfileMapper.delete(deleteWrapper);
                    System.out.println("删除原来的管理员档案");
                    
                    // 2. 更新公司的admin_user_id
                    company.setAdminUserId(existingUser.getId());
                    
                    // 3. 检查该用户在该企业是否已有档案
                    EmployeeProfile existingProfile = employeeProfileMapper.getByUserIdAndCompanyId(
                        existingUser.getId(), company.getId());
                    
                    if (existingProfile == null) {
                        // 创建新的员工档案
                        EmployeeProfile employeeProfile = new EmployeeProfile();
                        employeeProfile.setUserId(existingUser.getId());
                        employeeProfile.setCompanyId(company.getId());
                        employeeProfile.setRealName(company.getAdminName() != null ? company.getAdminName() : existingUser.getRealName());
                        employeeProfile.setUsername(existingUser.getUsername());
                        employeeProfile.setAvatarUrl(company.getAdminAvatarUrl() != null ? company.getAdminAvatarUrl() : existingUser.getAvatarUrl());
                        employeeProfile.setEmployeeCode(existingUser.getUsername());
                        employeeProfile.setStatus(1);
                        employeeProfileMapper.insert(employeeProfile);
                        System.out.println("为新用户创建员工档案");
                    } else {
                        // 更新现有档案
                        if (company.getAdminName() != null) {
                            existingProfile.setRealName(company.getAdminName());
                        }
                        if (company.getAdminAvatarUrl() != null) {
                            existingProfile.setAvatarUrl(company.getAdminAvatarUrl());
                        }
                        employeeProfileMapper.updateById(existingProfile);
                        System.out.println("更新现有员工档案");
                    }
                    
                } else {
                    // 场景1：新手机号对应的用户不存在，创建新用户
                    System.out.println("创建新用户，手机号: " + company.getAdminPhone());
                    
                    // 创建新的企业端用户
                    User newAdminUser = new User();
                    newAdminUser.setUsername(company.getAdminPhone());
                    newAdminUser.setPhone(company.getAdminPhone());
                    newAdminUser.setUserType("企业端");
                    newAdminUser.setStatus(1);
                    userMapper.insert(newAdminUser);
                    System.out.println("创建新用户，用户ID: " + newAdminUser.getId());
                    
                    // 删除原来的管理员档案
                    QueryWrapper<EmployeeProfile> deleteWrapper = new QueryWrapper<>();
                    deleteWrapper.eq("user_id", company.getAdminUserId())
                                 .eq("company_id", company.getId());
                    employeeProfileMapper.delete(deleteWrapper);
                    System.out.println("删除原来的管理员档案");
                    
                    // 更新公司的admin_user_id
                    company.setAdminUserId(newAdminUser.getId());
                    
                    // 创建新的员工档案
                    EmployeeProfile employeeProfile = new EmployeeProfile();
                    employeeProfile.setUserId(newAdminUser.getId());
                    employeeProfile.setCompanyId(company.getId());
                    employeeProfile.setRealName(company.getAdminName() != null ? company.getAdminName() : "管理员");
                    employeeProfile.setUsername(newAdminUser.getUsername());
                    employeeProfile.setAvatarUrl(company.getAdminAvatarUrl());
                    employeeProfile.setEmployeeCode(newAdminUser.getUsername());
                    employeeProfile.setStatus(1);
                    employeeProfileMapper.insert(employeeProfile);
                    System.out.println("创建新的员工档案");
                }
            } else if (company.getAdminUserId() != null) {
                // 没有修改手机号，只更新其他信息
                if (company.getAdminName() != null || company.getAdminAvatarUrl() != null) {
                    EmployeeProfile employeeProfile = new EmployeeProfile();
                    employeeProfile.setUserId(company.getAdminUserId());
                    employeeProfile.setCompanyId(company.getId());
                    if (company.getAdminName() != null) {
                        employeeProfile.setRealName(company.getAdminName());
                    }
                    if (company.getAdminAvatarUrl() != null) {
                        employeeProfile.setAvatarUrl(company.getAdminAvatarUrl());
                    }
                    
                    QueryWrapper<EmployeeProfile> updateWrapper = new QueryWrapper<>();
                    updateWrapper.eq("user_id", company.getAdminUserId())
                                 .eq("company_id", company.getId());
                    employeeProfileMapper.update(employeeProfile, updateWrapper);
                }
            }
            
            // 2. 更新公司基本信息（包括冗余的管理员信息）
            companyMapper.updateById(company);
            
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean updateStatus(Long companyId, Integer status) {
        if (companyId == null || status == null) return false;
        Company c = new Company();
        c.setId(companyId);
        c.setStatus(status); // 直接设置Integer类型
        return companyMapper.updateById(c) > 0;
    }
    
    @Override
    public List<Map<String, Object>> getIndustries() {
        // 硬编码行业列表，后续可以从数据库获取
        List<Map<String, Object>> industries = new ArrayList<>();
        String[] industryNames = {
            "互联网服务业", "电子商务", "软件开发", "制造业", "金融业",
            "教育培训", "医疗健康", "房地产", "物流运输", "其他"
        };
        
        for (int i = 0; i < industryNames.length; i++) {
            Map<String, Object> industry = new java.util.HashMap<>();
            industry.put("id", i + 1);
            industry.put("name", industryNames[i]);
            industry.put("code", "INDUSTRY_" + (i + 1));
            industries.add(industry);
        }
        return industries;
    }
    
    @Override
    @Transactional
    public boolean createCompanyWithAdmin(Company company) {
        try {
            // 1. 先创建公司记录（获得company_id）
            if (company.getTenantId() == null) {
                company.setTenantId(1L);
            }
            if (company.getCompanyCode() == null || company.getCompanyCode().isEmpty()) {
                company.setCompanyCode(generateCompanyCode());
            }
            company.setStatus(1); // 默认启用
            companyMapper.insert(company);
            
            // 2. 检查企业端用户是否存在（手机号 + 企业端）
            User existingUser = userMapper.selectByPhoneAndUserType(company.getAdminPhone(), "企业端");
            User adminUser;
            
            if (existingUser != null) {
                // 企业端用户已存在，直接使用
                adminUser = existingUser;
                System.out.println("企业端用户已存在，用户ID: " + adminUser.getId());
            } else {
                // 创建新的企业端用户（只存储认证相关信息）
                adminUser = new User();
                adminUser.setUsername(company.getAdminPhone()); // 使用管理员手机号作为用户名
                adminUser.setPhone(company.getAdminPhone()); // 管理员手机号
                adminUser.setUserType("企业端");
                // 注意：不设置company_id，因为一个用户可能对应多个企业
                adminUser.setStatus(1);
                userMapper.insert(adminUser);
                System.out.println("创建新的企业端用户，用户ID: " + adminUser.getId());
            }

            // 3. 检查该用户在该企业是否已有档案
            EmployeeProfile existingProfile = employeeProfileMapper.getByUserIdAndCompanyId(
                adminUser.getId(), company.getId());
            
            if (existingProfile == null) {
                // 创建新的员工档案（存储个人信息）
                EmployeeProfile employeeProfile = new EmployeeProfile();
                employeeProfile.setUserId(adminUser.getId());
                employeeProfile.setCompanyId(company.getId());
                employeeProfile.setRealName(company.getAdminName()); // 从company对象获取
                employeeProfile.setUsername(adminUser.getUsername());
                employeeProfile.setAvatarUrl(company.getAdminAvatarUrl()); // 从company对象获取
                employeeProfile.setEmployeeCode(adminUser.getUsername());
                employeeProfile.setStatus(1);
                employeeProfileMapper.insert(employeeProfile);
                System.out.println("创建新的员工档案");
            } else {
                System.out.println("员工档案已存在，跳过创建");
            }

            // 4. 更新公司的admin_user_id和管理员信息
            company.setAdminUserId(adminUser.getId());
            company.setAdminRealName(company.getAdminName()); // 使用company对象中的信息
            company.setAdminPhone(adminUser.getPhone());
            companyMapper.updateById(company);
            
            // 5. 为新公司创建管理员角色
           Role adminRole = createAdminRoleForCompany(company.getId());
            
            // 6. 绑定管理员角色
            if (adminRole != null) {
                UserRoleCompanyRelation relation = new UserRoleCompanyRelation();
                relation.setUserId(adminUser.getId());
                relation.setRoleId(adminRole.getId());
                relation.setCompanyId(company.getId());
                relation.setRelationshipType("admin");
                relation.setIsActive(1);
                userRoleCompanyRelationMapper.insert(relation);
            }
            
            return true;
        } catch (Exception e) {
            System.err.println("创建企业异常: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
    
    private String generateCompanyCode() {
        long timestamp = System.currentTimeMillis();
        String timestampStr = String.valueOf(timestamp).substring(5);
        int random = (int) (Math.random() * 1000);
        return "COMP" + timestampStr + String.format("%03d", random);
    }
    
    @Override
    @Transactional
    public boolean updateCompanyWithAdmin(Company company) {
        try {
            // 1. 先获取当前公司的信息，确保有adminUserId
            Company currentCompany = companyMapper.selectById(company.getId());
            if (currentCompany == null) {
                return false;
            }
            
            // 如果请求中没有adminUserId，使用数据库中的值
            if (company.getAdminUserId() == null) {
                company.setAdminUserId(currentCompany.getAdminUserId());
            }
            
            // 2. 如果修改了管理员手机号，需要重新处理用户关联
            if (company.getAdminUserId() != null && company.getAdminPhone() != null 
                && !company.getAdminPhone().equals(currentCompany.getAdminPhone())) {
                // 检查新手机号对应的用户是否存在
                User existingUser = userMapper.selectByPhoneAndUserType(company.getAdminPhone(), "企业端");
                
                if (existingUser != null) {
                    // 场景2：新手机号对应的用户已存在，切换到该用户
                    System.out.println("切换到已存在的用户，用户ID: " + existingUser.getId());
                    
                    // 1. 删除原来的管理员档案（如果存在）
                    QueryWrapper<EmployeeProfile> deleteWrapper = new QueryWrapper<>();
                    deleteWrapper.eq("user_id", company.getAdminUserId())
                                 .eq("company_id", company.getId());
                    employeeProfileMapper.delete(deleteWrapper);
                    System.out.println("删除原来的管理员档案");
                    
                    // 2. 更新公司的admin_user_id
                    company.setAdminUserId(existingUser.getId());
                    
                    // 3. 检查该用户在该企业是否已有档案
                    EmployeeProfile existingProfile = employeeProfileMapper.getByUserIdAndCompanyId(
                        existingUser.getId(), company.getId());
                    
                    if (existingProfile == null) {
                        // 创建新的员工档案
                        EmployeeProfile employeeProfile = new EmployeeProfile();
                        employeeProfile.setUserId(existingUser.getId());
                        employeeProfile.setCompanyId(company.getId());
                        employeeProfile.setRealName(company.getAdminName() != null ? company.getAdminName() : existingUser.getRealName());
                        employeeProfile.setUsername(existingUser.getUsername());
                        employeeProfile.setAvatarUrl(company.getAdminAvatarUrl() != null ? company.getAdminAvatarUrl() : existingUser.getAvatarUrl());
                        employeeProfile.setEmployeeCode(existingUser.getUsername());
                        employeeProfile.setStatus(1);
                        employeeProfileMapper.insert(employeeProfile);
                        System.out.println("为新用户创建员工档案");
                    } else {
                        // 更新现有档案
                        if (company.getAdminName() != null) {
                            existingProfile.setRealName(company.getAdminName());
                        }
                        if (company.getAdminAvatarUrl() != null) {
                            existingProfile.setAvatarUrl(company.getAdminAvatarUrl());
                        }
                        employeeProfileMapper.updateById(existingProfile);
                        System.out.println("更新现有员工档案");
                    }
                    
                } else {
                    // 场景1：新手机号对应的用户不存在，创建新用户
                    System.out.println("创建新用户，手机号: " + company.getAdminPhone());
                    
                    // 创建新的企业端用户
                    User newAdminUser = new User();
                    newAdminUser.setUsername(company.getAdminPhone());
                    newAdminUser.setPhone(company.getAdminPhone());
                    newAdminUser.setUserType("企业端");
                    newAdminUser.setStatus(1);
                    userMapper.insert(newAdminUser);
                    System.out.println("创建新用户，用户ID: " + newAdminUser.getId());
                    
                    // 删除原来的管理员档案
                    QueryWrapper<EmployeeProfile> deleteWrapper = new QueryWrapper<>();
                    deleteWrapper.eq("user_id", company.getAdminUserId())
                                 .eq("company_id", company.getId());
                    employeeProfileMapper.delete(deleteWrapper);
                    System.out.println("删除原来的管理员档案");
                    
                    // 更新公司的admin_user_id
                    company.setAdminUserId(newAdminUser.getId());
                    
                    // 创建新的员工档案
                    EmployeeProfile employeeProfile = new EmployeeProfile();
                    employeeProfile.setUserId(newAdminUser.getId());
                    employeeProfile.setCompanyId(company.getId());
                    employeeProfile.setRealName(company.getAdminName() != null ? company.getAdminName() : "管理员");
                    employeeProfile.setUsername(newAdminUser.getUsername());
                    employeeProfile.setAvatarUrl(company.getAdminAvatarUrl());
                    employeeProfile.setEmployeeCode(newAdminUser.getUsername());
                    employeeProfile.setStatus(1);
                    employeeProfileMapper.insert(employeeProfile);
                    System.out.println("创建新的员工档案");
                }
            } else if (company.getAdminUserId() != null) {
                // 没有修改手机号，只更新其他信息
                if (company.getAdminName() != null || company.getAdminAvatarUrl() != null) {
                    EmployeeProfile employeeProfile = new EmployeeProfile();
                    employeeProfile.setUserId(company.getAdminUserId());
                    employeeProfile.setCompanyId(company.getId());
                    if (company.getAdminName() != null) {
                        employeeProfile.setRealName(company.getAdminName());
                    }
                    if (company.getAdminAvatarUrl() != null) {
                        employeeProfile.setAvatarUrl(company.getAdminAvatarUrl());
                    }
                    
                    QueryWrapper<EmployeeProfile> updateWrapper = new QueryWrapper<>();
                    updateWrapper.eq("user_id", company.getAdminUserId())
                                 .eq("company_id", company.getId());
                    employeeProfileMapper.update(employeeProfile, updateWrapper);
                }
            }
            
            // 2. 设置公司的冗余字段
            company.setAdminRealName(company.getAdminName());
            company.setAdminPhone(company.getAdminPhone());
            
            // 3. 更新公司信息（包括所有字段：social_credit_code, industry, address 等）
            companyMapper.updateById(company);
            
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    public Company getCompanyWithAdminProfile(Company company) {
        if (company == null || company.getAdminUserId() == null) {
            return company;
        }
        
        try {
            // 从员工档案获取管理员详细信息
            QueryWrapper<EmployeeProfile> query = new QueryWrapper<>();
            query.lambda()
                .eq(EmployeeProfile::getUserId, company.getAdminUserId())
                .eq(EmployeeProfile::getCompanyId, company.getId());
            
            EmployeeProfile adminProfile = employeeProfileMapper.selectOne(query);
            if (adminProfile != null) {
                // 更新company对象中的管理员信息
                company.setAdminRealName(adminProfile.getRealName());
                company.setAdminAvatarUrl(adminProfile.getAvatarUrl());
            }
            
            return company;
        } catch (Exception e) {
            e.printStackTrace();
            return company; // 出错时返回原对象
        }
    }
    
    @Override
    @Transactional
    public boolean delete(Long companyId) {
        if (companyId == null) return false;
        
        try {
            // 1. 删除该公司的所有员工档案
            QueryWrapper<EmployeeProfile> profileWrapper = new QueryWrapper<>();
            profileWrapper.eq("company_id", companyId);
            employeeProfileMapper.delete(profileWrapper);
            
            // 2. 删除用户角色公司关系
            QueryWrapper<UserRoleCompanyRelation> relationWrapper = new QueryWrapper<>();
            relationWrapper.eq("company_id", companyId);
            userRoleCompanyRelationMapper.delete(relationWrapper);
            
            // 3. 删除该公司的角色
            QueryWrapper<org.example.entity.Role> roleWrapper = new QueryWrapper<>();
            roleWrapper.eq("company_id", companyId);
            roleMapper.delete(roleWrapper);
            
            // 4. 最后删除公司记录
            boolean companyDeleted = companyMapper.deleteById(companyId) > 0;
            
            return companyDeleted;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    public List<EmployeeProfile> getCompanyEmployees(Long companyId) {
        if (companyId == null) return new ArrayList<>();
        try {
            return employeeProfileMapper.getByCompanyId(companyId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
    
    @Override
    public int getCompanyEmployeeCount(Long companyId) {
        if (companyId == null) return 0;
        try {
            return employeeProfileMapper.countByCompanyId(companyId);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
    
    @Override
    public EmployeeProfile getEmployeeProfile(Long userId, Long companyId) {
        if (userId == null || companyId == null) return null;
        try {
            return employeeProfileMapper.getByUserIdAndCompanyId(userId, companyId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * 为新公司创建管理员角色
     */
    private org.example.entity.Role createAdminRoleForCompany(Long companyId) {
        try {
            // 先检查是否已存在该公司的管理员角色
            QueryWrapper<org.example.entity.Role> query = new QueryWrapper<>();
            query.lambda().eq(org.example.entity.Role::getCompanyId, companyId)
                    .like(org.example.entity.Role::getRoleCode, "ENTERPRISE_ADMIN");
            org.example.entity.Role existingRole = roleMapper.selectOne(query);
            
            if (existingRole != null) {
                return existingRole; // 已存在，直接返回
            }
            
            // 创建新的管理员角色
            Role adminRole = new Role();
            adminRole.setRoleName("企业管理员");
            adminRole.setRoleCode("ENTERPRISE_ADMIN_" + companyId); // 添加公司ID确保唯一性
            adminRole.setDescription("企业管理员，拥有企业所有权限");
            adminRole.setScope("tenant");
            adminRole.setApp("enterprise");
            adminRole.setCompanyId(companyId);
            adminRole.setIsActive(1);
            adminRole.setPermissions("[\"ent_list:view\", \"ent_list:create\", \"ent_list:edit\", \"ent_list:delete\", \"ent_user:view\", \"ent_user:create\", \"ent_user:edit\", \"ent_user:delete\"]");
            
            roleMapper.insert(adminRole);
            
            // 为企业管理员角色分配默认权限
            assignDefaultEnterprisePermissions(adminRole.getId());
            
            return adminRole;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * 为企业管理员角色分配默认权限
     */
    private void assignDefaultEnterprisePermissions(Long roleId) {
        try {
            // 获取企业端所有权限
            List<org.example.entity.Permission> enterprisePermissions = permissionMapper.selectList(
                new QueryWrapper<org.example.entity.Permission>()
                    .eq("app", "enterprise")
                    .eq("is_active", 1)
            );
            
            if (enterprisePermissions != null && !enterprisePermissions.isEmpty()) {
                List<Long> permissionIds = enterprisePermissions.stream()
                    .map(org.example.entity.Permission::getId)
                    .collect(java.util.stream.Collectors.toList());
                
                // 批量插入角色权限关联
                rolePermissionMapper.batchInsert(roleId, permissionIds);
                System.out.println("为企业管理员角色分配了 " + permissionIds.size() + " 个权限");
            }
        } catch (Exception e) {
            System.err.println("分配企业管理员默认权限失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}


